package com.zhongkai.service.config;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zhongkai.dao.config.ButtonDAO;
import com.zhongkai.model.config.Button;

@Component
@Transactional
public class ButtonService {
	private ButtonDAO buttonDAO;
	
	public void save(Button button) throws Exception{
		buttonDAO.insert(button);
	}
	
	public void delete(Button button) throws Exception{
		buttonDAO.delete(button);
	}
	
	public void update(Button button) throws Exception{
		buttonDAO.update(button);
	}
	
	public Button findById(Integer button_id) throws Exception{
		return (Button) buttonDAO.selectById(Button.class,button_id);
	}
	
	public Integer count() throws Exception{
		return buttonDAO.count("from Button");
	}
	
	@SuppressWarnings("unchecked")
	public List<Button> findAll() throws Exception{
		return buttonDAO.select("from Button");
	}
	
	@SuppressWarnings("unchecked")
	public boolean isExistActionKey(String actionKey) throws Exception{
		List<Button> buttons=buttonDAO.select("from Button b where b.actionKey='"+actionKey+"'");
		if(buttons==null||buttons.size()<1){
			return false;
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public Button findBytActionKey(String actionKey) throws Exception{
		if(actionKey==null||"".equals(actionKey)) return null;
		List<Button> buttons=buttonDAO.select("from Button b where b.actionKey='"+actionKey+"'");
		if(buttons!=null&&buttons.size()>0){
			return buttons.get(0);
		}
		return null;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public Button findByName(String buttonName) throws Exception{
		List<Button> buttons=buttonDAO.select("from Button b where b.buttonName='"+buttonName+"'");
		if(buttons==null||buttons.size()<1) return null;
		else return buttons.get(0);
	}
	
	

	public ButtonDAO getButtonDAO() {
		return buttonDAO;
	}

	@Resource
	public void setButtonDAO(ButtonDAO buttonDAO) {
		this.buttonDAO = buttonDAO;
	}

	
	

}
